import {useState} from "react";

// 超级管理员删除管理员

interface UseDeleteAdmin {
    deleteAdmin: (id: string, name: string) => Promise<void>;
    isLoading1: boolean;
    error1: string | null;
}

export function useDeleteAdmin(): UseDeleteAdmin {
    const [isLoading1, setisLoading1] = useState(false);
    const [error1, setError] = useState<string | null>(null);
    const token = localStorage.getItem('token');

    async function deleteAdmin(id: string, name: string): Promise<void> {
        setisLoading1(true);
        setError(null);
        try {
            if (!token) {
                setError('未提供授权令牌');
                return;
            }
            const response = await fetch('http://localhost:6868/superAdmin/deleteAdmin', {
                method: 'POST',
                headers: {
                    'Authorization': token!,
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    id: id,
                    name: name
                })
            });
            if (!response.ok) {
                throw new Error("删除管理员失败");
            }
        } catch (error1) {
            setError((error1 as Error).message);
        } finally {
            setisLoading1(false);
        }
    };

    return {deleteAdmin, isLoading1, error1};
}
